package br.org.etec.dotcao.listeners;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.persistence.NoResultException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import br.org.etec.dotcao.model.Animal;
import br.org.etec.dotcao.model.AnimalStatus;
import br.org.etec.dotcao.model.Race;
import br.org.etec.dotcao.model.RaceRepository;
import br.org.etec.dotcao.model.Repository;
import br.org.etec.dotcao.model.RepositoryFactory;
import br.org.etec.dotcao.model.Species;
import br.org.etec.dotcao.model.State;
import br.org.etec.dotcao.model.User;
import br.org.etec.dotcao.model.UserRepository;

@WebListener
public class InitDatabase implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void contextInitialized(ServletContextEvent event) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				getRunnableRootUser().run();
				getRunnableStates().run();
				getRunnableSpecies().run();
				getRunnabçeAnimals().run();
			}
		}).start();
	}
	
	private Runnable getRunnabçeAnimals(){
		return new Runnable() {
			@Override
			public void run() {
				System.out.println("Checando o cadastro de animais....");
				
				Repository<Animal> repository = RepositoryFactory.get(Animal.class);
				RaceRepository rrepo = (RaceRepository) RepositoryFactory.get(Race.class);
				
				List<Animal> stored = repository.all();
				if(stored.size()== 0 || stored.size() == 1){
					Animal nina = new Animal();
					nina.setAge("8 anos");
					nina.setColor("Marrom");
					nina.setDescription("Fofinha, meiga, carinhosa...");
					nina.setGender("F");
					nina.setName("Nina");
					nina.setRace(rrepo.get("Bassethound"));
					nina.setWeight("medium");
					nina.setStatus(AnimalStatus.ADOPTION_AVAYLABLE);
					nina.save();

					Animal sam = new Animal();
					sam.setAge("2 anos");
					sam.setColor("Marrom");
					sam.setGender("M");
					sam.setName("Sam");
					sam.setRace(rrepo.get("Collie"));
					nina.setWeight("medium");
					sam.setStatus(AnimalStatus.ADOPTION_AVAYLABLE);
					sam.save();

					Animal corona = new Animal();
					corona.setAge("2 anos");
					corona.setColor("Marrom");
					corona.setGender("M");
					corona.setName("Sam");
					nina.setWeight("medium");
					corona.setStatus(AnimalStatus.ADOPTION_AVAYLABLE);
					corona.setRace(rrepo.get("Labrador"));
					corona.save();

					Animal tita = new Animal();
					tita.setAge("1 anos");
					tita.setColor("Marrom");
					tita.setGender("F");
					nina.setWeight("medium");
					tita.setName("Tita");
					tita.setStatus(AnimalStatus.ADOPTION_AVAYLABLE);
					tita.setRace(rrepo.get("Dálmata"));
					tita.save();

					Animal beethoven = new Animal();
					beethoven.setAge("2 anos");
					beethoven.setColor("Marrom e Branco");
					beethoven.setGender("M");
					beethoven.setName("Beethoven");
					nina.setWeight("larger");
					beethoven.setStatus(AnimalStatus.ADOPTION_AVAYLABLE);
					beethoven.setRace(rrepo.get("São Bernardo"));
					beethoven.save();
				}
			}
		};
	}

	private Runnable getRunnableSpecies() {
		return new Runnable() {
			@Override
			public void run() {
				System.out.println("Checando o cadastro de espécies e raças...");
				Repository<Species> repository = RepositoryFactory.get(Species.class);
				
				if(repository.all().size() == 0){
					Species caninos = new Species();
					caninos.setNome("Caninos");
					caninos.addRace(new Race("Afghan Hound", ""));
					caninos.addRace(new Race("Airedale Terrier", ""));
					caninos.addRace(new Race("Akita Inu", ""));
					caninos.addRace(new Race("American Pitbull", ""));
					caninos.addRace(new Race("Bassethound", ""));
					caninos.addRace(new Race("Beagle", ""));
					caninos.addRace(new Race("Bichon Frisé", ""));
					caninos.addRace(new Race("Boxer", ""));
					caninos.addRace(new Race("Bulldog", ""));
					caninos.addRace(new Race("Bullmastiff", ""));
					caninos.addRace(new Race("Bull Terrier", ""));
					caninos.addRace(new Race("Chihuahua", ""));
					caninos.addRace(new Race("Chow Chow", ""));
					caninos.addRace(new Race("Cocker Americano", ""));
					caninos.addRace(new Race("Cocker Inglês", ""));
					caninos.addRace(new Race("Collie", ""));
					caninos.addRace(new Race("Dálmata", ""));
					caninos.addRace(new Race("Dobermann", ""));
					caninos.addRace(new Race("Dogue Alemão", ""));
					caninos.addRace(new Race("Fila Brasileiro", ""));
					caninos.addRace(new Race("Foxhound Inglês", ""));
					caninos.addRace(new Race("Fox Terrier Pelo Duro", ""));
					caninos.addRace(new Race("Fox Terrier Pelo Liso", ""));
					caninos.addRace(new Race("Golden Retriever", ""));
					caninos.addRace(new Race("Husky Siberiano", ""));
					caninos.addRace(new Race("Labrador", ""));
					caninos.addRace(new Race("Lhasa Apso", ""));
					caninos.addRace(new Race("Maltês", ""));
					caninos.addRace(new Race("Mastiff", ""));
					caninos.addRace(new Race("Mastino Napoletano", ""));
					caninos.addRace(new Race("O. E. Sheepdog", ""));
					caninos.addRace(new Race("Pastor Alemão", ""));
					caninos.addRace(new Race("Pequinês", ""));
					caninos.addRace(new Race("Perdigueiro", ""));
					caninos.addRace(new Race("Pinscher", ""));
					caninos.addRace(new Race("Pointer Inglês", ""));
					caninos.addRace(new Race("Poodle", ""));
					caninos.addRace(new Race("Rottweiler", ""));
					caninos.addRace(new Race("São Bernardo", ""));
					caninos.addRace(new Race("Schnauzer", ""));
					caninos.addRace(new Race("Setter Inglês", ""));
					caninos.addRace(new Race("Setter Irlandês", ""));
					caninos.addRace(new Race("Shar Pei", ""));
					caninos.addRace(new Race("Shih Tzu", ""));
					caninos.addRace(new Race("Spitz Alemão", ""));
					caninos.addRace(new Race("Staff Bull Terrier", ""));
					caninos.addRace(new Race("Teckel", ""));
					caninos.addRace(new Race("Terrier Brasileiro", ""));
					caninos.addRace(new Race("Yorkshire Terrier", ""));
					caninos.save();
					
					Species felinos = new Species();
					felinos.setNome("Felinos");
					felinos.addRace(new Race("Abissínio", ""));
					felinos.addRace(new Race("American Shorthair", ""));
					felinos.addRace(new Race("Angorá", ""));
					felinos.addRace(new Race("Bengal", ""));
					felinos.addRace(new Race("Brazilian Shorthair", ""));
					felinos.addRace(new Race("British Shorthair", ""));
					felinos.addRace(new Race("Cornish Rex", ""));
					felinos.addRace(new Race("Egyptian Mau", ""));
					felinos.addRace(new Race("Exótico", ""));
					felinos.addRace(new Race("Himalaio", ""));
					felinos.addRace(new Race("Maine Coon", ""));
					felinos.addRace(new Race("Norwegian Forest", ""));
					felinos.addRace(new Race("Oriental", ""));
					felinos.addRace(new Race("Persa", ""));
					felinos.addRace(new Race("Ragdoll", ""));
					felinos.addRace(new Race("Sagrado da Birmânia", ""));
					felinos.addRace(new Race("Scottish Fold", ""));
					felinos.addRace(new Race("Siamês", ""));
					felinos.addRace(new Race("Sphynx", ""));
					felinos.save();
				}
			}
		};
	}

	private Runnable getRunnableRootUser() {
		return new Runnable() {
			@Override
			public void run() {
				System.out.println("Checando a existência do super-usuário do sistema....");
				UserRepository urepository = (UserRepository) RepositoryFactory.get(User.class);
				try {
					if(urepository.getByEmail("root@dotcao.com.br") == null){
						createRootUser();
					}
				} catch (NoResultException e) {
					createRootUser();
				}
			}
		};
	}

	private Runnable getRunnableStates() {
		return new Runnable() {
			@Override
			public void run() {
				System.out.println("Checando o cadastro de Estados Brasileiros...");
				Repository<State> srepository = RepositoryFactory.get(State.class);
				if(srepository.all().size() == 0){
					new State("Acre", "AC").save();
					new State("Amapá", "AP").save();
					new State("Alagoas", "AL").save();
					new State("Amazonas", "AM").save();
					new State("Bahia", "BA").save();
					new State("Ceará", "CE").save();
					new State("Distrito Federal", "DF").save();
					new State("Espírito Santo", "ES").save();
					new State("Goiás", "GO").save();
					new State("Maranhão", "MA").save();
					new State("Mato Grosso", "MT").save();
					new State("Mato Grosso do Sul", "MS").save();
					new State("Minas Gerais", "MG").save();
					new State("Pará", "PA").save();
					new State("Paraíba", "PB").save();
					new State("Paraná", "PR").save();
					new State("Pernambuco", "PE").save();
					new State("Piauí", "PI").save();
					new State("Rio de Janeiro", "RJ").save();
					new State("Rio Grande do Norte", "RN").save();
					new State("Rio Grande do Sul", "RS").save();
					new State("Rondônia", "RO").save();
					new State("Roraima", "RR").save();
					new State("Santa Catarina", "SC").save();
					new State("São Paulo", "SP").save();
					new State("Sergipe", "SE").save();
					new State("Tocantins", "TO").save();
				}
			}
		};
	}
	
	private void createRootUser(){
		User user = new User();
		
		try {
			user.setBirthday(new SimpleDateFormat("dd/MM/yyyy").parse("09/10/1984"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		user.setFirstname("Administrador");
		user.setGender("M");
		user.setLastname("do Sistema");
		user.setPassword("123456");
		user.setUsername("root@dotcao.com.br");
		user.save();
	}

}
